﻿using System.Collections.Generic;
using System.Linq;
using System.Configuration;

namespace SuddenDeath.Domain.Services.FileManager.elFinder.Configuration
{
    public static class Configuration
    {
        public static bool IsLoaded;

        public static string RootPath { get; private set; }

        public static string RootUrl { get; private set; }

        public static string DotFiles { get; private set; }

        public static string UplMaxSize { get; private set; }

        public static IEnumerable<string> DisabledCommands { get; private set; }

        public static IEnumerable<string> DisabledMimeTypes { get; private set; }

        public static void Init(System.Web.HttpContext context)
        {
            if (IsLoaded)
                return;

            var section = (ElFinderSection)ConfigurationManager.GetSection("ElFinder");

            RootPath = context.Server.MapPath(section.Root.Path);

            RootUrl = section.Root.Url;

            DotFiles = section.DotFiles;

            UplMaxSize = section.UplMaxSize + "M";

            DisabledCommands = section.DisabledCommands.Count > 0 ? 
                section.DisabledCommands.Cast<NamedElement>().Where(x=>x.Name!=string.Empty).Select(x => x.Name) : 
                new List<string>();

            DisabledMimeTypes = section.DisabledMimeTypes.Count > 0 ? 
                section.DisabledMimeTypes.Cast<NamedElement>().Where(x => x.Name != string.Empty).Select(x => x.Name) : 
                new List<string>();
        }
    }
}
